package com.example.projectmanagement.repository;

import com.example.projectmanagement.model.UserRole;
import com.example.projectmanagement.model.User;
import com.example.projectmanagement.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

@Repository
public interface UserRoleRepository extends JpaRepository<UserRole, Long> {

    Optional<UserRole> findByUserAndRole(User user, Role role);
    
    @Query("SELECT ur.role FROM UserRole ur WHERE ur.user.id = :userId")
    List<Role> findRolesByUserId(@Param("userId") Long userId);
    
    // 根据角色ID查找关联的用户角色
    List<UserRole> findByRole_Id(Long roleId);
    
    void deleteByUser_Id(Long userId);
    
    // 检查用户角色关联是否存在
    boolean existsByUser_IdAndRole_Id(Long userId, Long roleId);
}